package pl.edu.agh.io.gpstracker.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import pl.edu.agh.io.gpstracker.client.dto.UserDto;
import pl.edu.agh.io.gpstracker.client.dto.UserType;
import pl.edu.agh.io.gpstracker.dao.UserDao;
import pl.edu.agh.io.gpstracker.model.User;

/**
 * 
 * @author wkedzior tak nie powinno byc - ale z powodu braku czasu, ustawiam
 *         transakcyjnosc, dla wszystkich metod.
 */
@Service(value="userService")
@Transactional
public class UserServiceImpl extends GenericService<User, UserDto> implements
		UserService {

	public UserServiceImpl() {
		super(User.class, UserDto.class);
	}

	@Autowired
	private UserDao userDao;

	@Transactional
	public void addNewUser(UserDto user) throws DataAccessException {
		userDao.saveOrUpdate(mapDto(user));
	}

	@Transactional(readOnly=true)
	public UserDto login(String login, String password)
			throws DataAccessException {
		return mapEntity(userDao.login(login, password));
	}

	@Transactional(readOnly=true)
	public List<UserDto> findAllUsers(UserType type) throws DataAccessException {
		return mapEntityList((userDao.findAllByType(type != null ? (short) type
				.getValue() : null)));
	}
	
	@Transactional
	public void deleteUser(Long id) throws DataAccessException {
		userDao.deleteById(id);
	}
	
	@Transactional
	public void updateUsers(List<UserDto> users) throws DataAccessException {
		for (UserDto userDto: users){;
			userDao.saveOrUpdate(mapDto(userDto));
		}
	}
	

}
